package com.hitenine.controller;


import com.hitenine.pojo.Users;
import com.hitenine.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    //跳转到修改页面
    @RequestMapping("/toUpdateUser")
    public String toUpdatePaper(int id,Model model) {
        Users users = userService.queryUserByID(id);
        model.addAttribute("Tuser", users);
        return "updateUser";
    }
    //修改用户的请求
    @RequestMapping("/updateUser")
    public String updateUser(Users users) {
        System.out.println("updateUser=>" + users);
        userService.updateUser(users);
        return "redirect:/book/allBook";    //重定向我们的@RequestMapping("/allBook")请求
    }

    //登录的请求
    @RequestMapping("/queryUser")
    public String queryUser(@RequestParam(value="username")String username ,
                            @RequestParam(value="psw")String password, HttpSession session,Model model) {
        Users users = new Users();
        users.setUserName(username);
        users.setPassWord(password);
        Users users1 = userService.queryUser(users);
        if(users1 != null){
            session.setAttribute("Tuser", users1);
            return "redirect:/book/allBook"; //重定向我们的@RequestMapping("/allBook")请求
        }else{
            model.addAttribute("error", "用户名或密码错误");
            return "index";
        }

    }
}
